package com.codeman.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeman.business.book.domain.BookCategory;
import com.codeman.business.book.domain.BookIndex;
import com.codeman.business.book.mapper.BookContentMapper;
import com.codeman.business.book.mapper.BookMapper;
import com.codeman.business.book.service.IBookCategoryService;
import com.codeman.business.book.service.IBookContentService;
import com.codeman.business.book.service.IBookIndexService;
import com.codeman.business.book.service.IBookService;
import com.codeman.business.user.service.IUserBookshelfService;
import com.codeman.business.user.service.IUserReadHistoryService;
import com.codeman.common.core.controller.BaseController;
import com.codeman.common.core.domain.AjaxResult;
import com.codeman.common.core.page.TableDataInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("book")
public class BookController extends BaseController {
    @Autowired
    private IBookService bookService;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private IBookCategoryService bookCategoryService;

    @Autowired
    private IBookIndexService bookIndexService;

    @Autowired
    private BookContentMapper bookContentMapper;

    @Autowired
    private IUserBookshelfService userBookshelfService;
    @Autowired
    private IUserReadHistoryService userReadHistoryService;


    /**
     * 查询小说分类列表
     */
    @GetMapping("listBookCategory")
    public TableDataInfo listBookCategory() {
        return getDataTable(bookCategoryService.list());
    }

    /**
     * 查询首页新书榜单数据
     */
    @GetMapping("listRank/new")
    public TableDataInfo listNewRank(@RequestParam(value = "catId")Integer catId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        if(null!=catId){
            queryWrapper.eq("cat_id",catId);
        }
        return getDataTable(bookService.list(queryWrapper));
    }

    /**
     * 查询首页更新榜单数据
     */

    /**
     * 查询首页更新榜单数据
     */
    @GetMapping("listRank/update")
    public TableDataInfo listUpdateRank() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("update_time");
        return getDataTable(bookService.list(queryWrapper));
    }

    /**
     * 查询小说详情信息
     */
    @GetMapping("queryBookDetail/{id}")
    public AjaxResult queryBookDetail(@PathVariable("id") long id) {

        return AjaxResult.success(bookService.getById(id));
    }

    /**
     * 根据小说ID查询小说前十条最新更新目录集合
     */
    @GetMapping("queryNewIndexList")
    public AjaxResult queryNewIndexList(Long bookId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("update_time");
        Page<BookIndex> page = new Page<>(1, 10);
        IPage<BookIndex> list = bookIndexService.page(page, queryWrapper);
        return AjaxResult.success(list.getRecords());
    }

    /**
     * 目录页
     */
    @GetMapping("/queryIndexList")
    public TableDataInfo indexList(@RequestParam(value = "bookid")long bookId, @RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "15") int pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("book_id", bookId);
        Page<BookIndex> pg = new Page<>(page, pageSize);
        IPage list = bookIndexService.page(pg, queryWrapper);
        return getDataTable(list.getRecords());
    }

    /**
     * 增加点击次数
     */
    @PostMapping("addVisitCount")
    public AjaxResult addVisitCount(Long bookId) {
        bookMapper.addVisitCount(bookId, 1);
        return AjaxResult.success();
    }

    /**
     * 目录页
     */
    @GetMapping("/queryContent/{bookId}/{indexNum}")
    public AjaxResult content(@PathVariable(value = "bookId")long bookId,@PathVariable(value = "indexNum")int indexNum) {
        return AjaxResult.success(bookContentMapper.getContent(bookId,indexNum));
    }


    /**
     * 分页搜索
     */
//    @GetMapping("searchByPage")
//    public ResultBean searchByPage(BookSP bookSP, @RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "20") int pageSize) {
//        PageInfo<BookVO> pageInfo = bookService.searchByPage(bookSP, page, pageSize);
//        return ResultBean.ok(pageInfo);
//    }


    /**
     * 查询章节相关信息
     */
//    @GetMapping("queryBookIndexAbout")
//    public ResultBean queryBookIndexAbout(Long bookId, Long lastBookIndexId) {
//        Map<String, Object> data = new HashMap<>(2);
//        data.put("bookIndexCount", bookService.queryIndexCount(bookId));
//        String lastBookContent = bookService.queryBookContent(lastBookIndexId).getContent();
//        if (lastBookContent.length() > 42) {
//            lastBookContent = lastBookContent.substring(0, 42);
//        }
//        data.put("lastBookContent", lastBookContent);
//        return ResultBean.ok(data);
//    }


//    /**
//     * 分页查询书籍评论列表
//     */
//    @GetMapping("listCommentByPage")
//    public ResultBean listCommentByPage(@RequestParam("bookId") Long bookId, @RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "5") int pageSize) {
//        return ResultBean.ok(new PageInfo<>(bookService.listCommentByPage(null, bookId, page, pageSize)));
//    }
//
//    /**
//     * 新增评价
//     */
//    @PostMapping("addBookComment")
//    public ResultBean addBookComment(BookComment comment, HttpServletRequest request) {
//        UserDetails userDetails = getUserDetails(request);
//        if (userDetails == null) {
//            return ResultBean.fail(ResponseStatus.NO_LOGIN);
//        }
//        bookService.addBookComment(userDetails.getId(), comment);
//        return ResultBean.ok();
//    }

}
